package com.ccut;

import static com.ccut.Constant.ShapeType.CIRCLE;
import static java.lang.Math.PI;

public class Circle extends MyShape{

    private double radius;

    public Circle(double radius) {
        super(CIRCLE);
        this.radius = radius;
        setPerimeter(calculatePerimeter());
        setArea(calculateArea());
        addShape(this);
    }

    @Override
    public double calculatePerimeter() {
        return 2 * PI * radius;
    }

    @Override
    public double calculateArea() {
        return PI * radius * radius;
    }

    @Override
    public String toString() {
        return "图形的类型:"+ getShapeType().getChineseName() + "\n" +
                "图形的半径:"+ radius + "\n" +
                "图形的周长(近似):"+ String.format("%.2f", getPerimeter()) + "\n" +
                "图形的面积(近似):"+ String.format("%.2f", getArea());
    }

    @Override
    public void showShape() {
        // 创建Swing图形界面来绘制圆形
        javax.swing.SwingUtilities.invokeLater(() -> {
            // 创建主窗口
            javax.swing.JFrame frame = new javax.swing.JFrame("圆形绘制");
            frame.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);

            // 创建绘图面板
            javax.swing.JPanel panel = new javax.swing.JPanel() {
                @Override
                protected void paintComponent(java.awt.Graphics g) {
                    super.paintComponent(g);
                    java.awt.Graphics2D g2d = (java.awt.Graphics2D) g;

                    // 设置抗锯齿
                    g2d.setRenderingHint(
                            java.awt.RenderingHints.KEY_ANTIALIASING,
                            java.awt.RenderingHints.VALUE_ANTIALIAS_ON
                    );

                    // 获取面板尺寸
                    int width = getWidth();
                    int height = getHeight();

                    // 设置缩放比例，让半径为100的圆大约占窗口的一半
                    double scale = Math.min(width, height) / 200.0;
                    int scaledRadius = (int) (radius * scale);

                    // 确保圆形在可视区域内
                    int centerX = width / 2;
                    int centerY = height / 2;

                    // 调整中心点，确保大圆不会超出边界
                    int displayRadius = Math.min(scaledRadius, (int)(Math.min(width, height) * 0.45));
                    int displayCenterX = width / 2;
                    int displayCenterY = height / 2;

                    // 绘制圆形
                    g2d.setColor(java.awt.Color.BLUE);
                    g2d.drawOval(displayCenterX - displayRadius, displayCenterY - displayRadius,
                            displayRadius * 2, displayRadius * 2);

                    // 填充圆形
                    g2d.setColor(new java.awt.Color(0, 0, 255, 50));
                    g2d.fillOval(displayCenterX - displayRadius, displayCenterY - displayRadius,
                            displayRadius * 2, displayRadius * 2);

                    // 绘制信息文本 - 使用支持中文的字体
                    g2d.setColor(java.awt.Color.BLACK);
                    // 使用系统默认的支持中文的字体
                    java.awt.Font font = new java.awt.Font("微软雅黑", java.awt.Font.PLAIN, 12);
                    // 如果微软雅黑不可用，则尝试其他中文字体
                    if (font.getName().equals("微软雅黑")) {
                        font = new java.awt.Font("宋体", java.awt.Font.PLAIN, 12);
                    }
                    // 如果还是不行，使用系统默认字体
                    if (font.getName().equals("宋体")) {
                        font = new java.awt.Font(java.awt.Font.DIALOG, java.awt.Font.PLAIN, 12);
                    }
                    g2d.setFont(font);
                    g2d.drawString("圆形 (半径: " + String.format("%.2f", radius) + ")", 20, 20);
                    g2d.drawString("周长: " + String.format("%.2f", getPerimeter()), 20, 40);
                    g2d.drawString("面积: " + String.format("%.2f", getArea()), 20, 60);
                }
            };

            // 添加面板到窗口
            frame.add(panel);
            frame.setSize(500, 500);  // 增大窗口以便更好地显示不同大小的圆
            frame.setLocationRelativeTo(null); // 居中显示
            frame.setVisible(true);
        });
    }
}
